/**
 * 定义AlarmMangerController污水报警信息模块
 */
(function(window, angular, undefined){
	
	var AlarmMangerController = angular.module('AlarmMangerController', ['ui.router','itfreer','waterCommon','OperatorManagerService']);
	AlarmMangerController.controller('AlarmInfoController',function(DateService,$TaskNavService,$state,$scope,$waterDataCommon,$wdp,$rootScope,OperationStationJobService,$state,$itfreerHttp){
		
		/**
		 * 报警信息发送详情
		 */
		$scope.alarmMessageDetail = null;
		//去掉视频延迟函数
		if($wdp.timeout){
			clearTimeout($wdp.timeout); 
		}
		$scope.queryObject={
				taskCode:$wdp.viewValue.taskcode
		};
		$scope.stationType = "0";
		$scope.alarmLog = "0";
		//$scope.stationType = "allType";
		
		$scope.alarmType = [
		                    {name:"全部",value:""},
		                     {name:"仪器故障",value:"deviceError"},
		                     {name:"数据异常",value:"dataError"}
		                     ];
		
		
		
		$scope.alarmDataSource = "redis";
		$scope.alarmDataSourceData = [
		                         {"k":"5分钟数据","v":"5分钟数据"},
		                         {"k":"小时数据","v":"小时数据"},
		                         {"k":"redis","v":"实时数据"},
		                         {"k":"天数据","v":"天数据"}
		                     ];
		
		$scope.onAlarmTypeChange = function(){
			if($scope.alarmType=="allType")
			{
				$scope.alarmDataSourceData = [{"k":"allType","v":"全部"},{"k":"5分钟数据","v":"5分钟数据"},{"k":"小时数据","v":"小时数据"},
				                              {"k":"redis","v":"实时数据"},
				                              {"k":"天数据","v":"天数据"}];
				$scope.alarmDataSource="allType";
			}
			if($scope.alarmType=="1")
			{
				$scope.alarmDataSourceData = [{"k":"allType","v":"全部"},{"k":"5分钟数据","v":"5分钟数据"},{"k":"小时数据","v":"小时数据"}];
				$scope.alarmDataSource="allType";
			}
			if($scope.alarmType=="3"||$scope.alarmType=="4")
			{
				$scope.alarmDataSourceData = [{"k":"redis","v":"实时数据"}];
				$scope.alarmDataSource="redis";
			}
			if($scope.alarmType=="7"||$scope.alarmType=="8")
			{
				$scope.alarmDataSourceData = [{"k":"小时数据","v":"小时数据"}];
				$scope.alarmDataSource="小时数据";
			}
			if($scope.alarmType=="9")
			{
				$scope.alarmDataSourceData = [{"k":"天数据","v":"天数据"}];
				$scope.alarmDataSource="天数据";
			}
			//进行查询
			$scope.getAlarmInfoList();
		}
		
		
		//初始化日期
		$scope.initQuery = function(){
			var nowDate = new Date();
			var month="";
			var day="";
			var curMonthDays = new Date(nowDate.getFullYear(), (nowDate.getMonth()), 0).getDate();//获取当月有多少天
			if(nowDate.getDate()-1==0){
				month=nowDate.getMonth();
				day=curMonthDays;
			}else{
				month=nowDate.getMonth()+1;
				day=nowDate.getDate();
			}
			$scope.queryObject.start=nowDate.getFullYear()+"-"+month+"-"+day;
			$scope.queryObject.alarmLogDate=nowDate.getFullYear()+"-"+month+"-"+day;
		}
		$scope.initQuery();

		
		//监听左侧列表
	    $scope.$on('dataPramChanged', function(event, data) {
	    	if($scope.firstQuery){
				$scope.allStationSelected = false;
			}
			else{
				$scope.allStationSelected = false;
			}
			$scope.firstQuery = false;
	    	$wdp.viewValue.wwtpDetailParm = data;
			$scope.STATION_NAME=$wdp.viewValue.wwtpDetailParm.STATION_NAME;
			$scope.STATION_ID = $wdp.viewValue.wwtpDetailParm.STATION_ID;
			$scope.queryObject.STATION_ID = $scope.STATION_ID;
			$scope.getAlarmInfoList();
			$("#wsclcname li[orgid='"+1+"']").removeClass('selected');
			$("#wsclcname li[orgid='"+0+"']").addClass('selected');
	    });
		$scope.checkErr = function(){		   
		    if((new Date($scope.queryObject.start.replace(/-/g,"\/"))) > (new Date($scope.queryObject.end.replace(/-/g,"\/")))){
		    	$.messager.alert("提示","起始时间不能大于结束时间");
		      return false;
		    }		 
		  };
	    $scope.getStationname=function(orgid){
			if(orgid=="0"){
				$("#wsclcname li[orgid='"+1+"']").removeClass('selected');
				$("#wsclcname li[orgid='"+0+"']").addClass('selected');
				$scope.queryObject.WWTP_ID=$wdp.viewValue.wwtpDetailParm.WWTPID;	
				$scope.queryObject.STATION_ID=$wdp.viewValue.wwtpDetailParm.STATION_ID;
				$scope.allStationSelected=false;
			}
            if(orgid=="1"){
            	$("#wsclcname li[orgid='"+0+"']").removeClass('selected');
            	$("#wsclcname li[orgid='"+1+"']").addClass('selected');
            	$scope.queryObject.STATION_ID="";
            	$scope.queryObject.WWTP_ID="";
            	$scope.allStationSelected=true;
			}
		};
		var now = new Date();

		//报警类型条件
		$scope.alarmInfoTypeData = [];
		//报警等级
		$scope.alarmInfoLevelData=[];
		$scope.alarmInfoLevelData.push({"k":"allType","v":"全部"});
		$scope.alarmInfoLevelData.push({"k":"1","v":"一级"});
		$scope.alarmInfoLevelData.push({"k":"2","v":"二级"});
		$scope.alarmInfoLevelData.push({"k":"3","v":"三级"});
		//状态
		$scope.alarmStatuseData = [];
		$scope.alarmStatuseData.push({"k":"allType","v":"全部"});
		$scope.alarmStatuseData.push({"k":"1","v":"已恢复"});
		$scope.alarmStatuseData.push({"k":"0","v":"未恢复"});
		$scope.alarmType = "allType";
		$scope.alarmLevel="allType";
		$scope.alarmStatus="0";
		$scope.recoverType="allType";
		$scope.allStationSelected = false;
		$scope.firstQuery = false;
		$scope.STATION_NAME="";
		$scope.STATION_ID="";
		OperationStationJobService.postQuery("/Exception/getAlarmInfoTypeNew.do",{}).then(function(data){
			$scope.alarmInfoTypeData.push({"k":"allType","v":"全部"});
			for(var i=0;i<data.data.length;i++){
				$scope.alarmInfoTypeData.push({"k":data.data[i].S_DOMAINCODE,"v":data.data[i].S_DOMAINCAPTION});
			}
		});
		$scope.getAlarmInfo=function(parm){
			$scope.allStationSelected=parm;
			$scope.queryObject.STATION_ID=$wdp.viewValue.wwtpDetailParm.STATION_ID;
			$scope.getAlarmInfoList();
		}
		//获取报警信息
		$scope.getAlarmInfoList = function(){
			if($scope.allStationSelected == true){
			   $scope.queryObject.STATION_ID = null;
			}else{
				$scope.queryObject.STATION_ID = $scope.STATION_ID;
			}
			
			$scope.queryObject.ALARM_TYPE=$scope.alarmType;
			$scope.queryObject.IS_RECOVER=$scope.alarmStatus;
			$scope.queryObject.recoverType = $scope.recoverType;
			$scope.queryObject.ALARM_DATASOURCE=$scope.alarmDataSource;
			if($scope.alarmType=='allType'){
				delete $scope.queryObject.ALARM_TYPE;
			}	
			$scope.queryObject.CONTINUE_TIME=$scope.alarmLevel;
			if($scope.alarmLevel=='allType'){
				delete $scope.queryObject.CONTINUE_TIME;
			}
			if($scope.alarmStatus=='allType'){
				delete $scope.queryObject.IS_RECOVER;
			}
			if($scope.alarmDataSource=='allType'){
				delete $scope.queryObject.ALARM_DATASOURCE;
			}
			
			$waterDataCommon.getPageRows("/Exception/getAlarmInfoLevelList.do","AlarmInfoList",$scope.queryObject,12,1);
		}
		//显示等级颜色
		$scope.showColor=function(data,str,type){
			if((str=="1"&&type=="1")||(str=="2"&&type=="1")||(str=="3"&&type=="1")){
				return 'levelone';
			}
			else if((str=="2"&&type=="2")||(str=="3"&&type=="2")){
				return 'leveltwo';
			}
			else if(str=="3"&&type=="3"){
				return 'levelthree';
			}
			else {
				return 'levelColorGray';
			}
		}
		//查看处置
		$scope.viewAlarmProcess = function(curRow){
			$wdp.viewValue.ALARM_ID = curRow.GUID;
			$state.go("AlarmInfo.alarmProcess",{alarmId:curRow.GUID,fromState:'AlarmInfo.main'});
		}
		
		//设置弹窗已提醒
		$scope.setIsPopup = function(curRow){
			var guid = curRow.GUID;
            OperationStationJobService.postQuery("/Data/Update.do",{tableName:'OP_ALAR_INFO',GUID:guid,IS_POPUP:'1'}).then(function(data){
            	$scope.getAlarmInfoList();
            });
		}
		$scope.BackTOTask=function(){
			$TaskNavService.BackToTask();
		};
		
		//设置报警等级
		$scope.setAlarmLevel=function(){
			$scope.getLevelTime();
			$itfreer.openWindow('AlarmLevelSet');
		};
		//取消报警等级
		$scope.cancelLevelData=function(){
			$itfreer.closeWindow('AlarmLevelSet');
		};
		//获取等级时长
		$scope.leveone="60";
		$scope.leveltwo="120";
		$scope.levelthree="1440";
		$scope.getLevelTime=function(){
			$waterDataCommon.getPageRows("/Exception/getLevelTime.do","time",$scope.queryObject,10,1,function(d){
				if(d.data.length>0){
					var obj=d.data[0];
					$scope.leveone=obj.LEVEL_ONE;
					$scope.leveltwo=obj.LEVEL_TWO;
					$scope.levelthree=obj.LEVEL_THREE;
				}
			})
		};
		//保存报警等级
		$scope.levelsetData={};
		$scope.saveLevelData=function(){
			$scope.levelsetData.WWTP_ID= $wdp.viewValue.wwtpDetailParm.WWTPID;
			$scope.levelsetData.LEVEL_ONE=$scope.leveone;
			$scope.levelsetData.LEVEL_TWO=$scope.leveltwo;
			$scope.levelsetData.LEVEL_THREE=$scope.levelthree;
			$itfreerHttp.save('BC_ALARM_TM_SET',$scope.levelsetData,function(d){
				$scope.getAlarmInfoList();
				$itfreer.closeWindow('AlarmLevelSet');
				$.messager.alert("提示","保存成功","info");
			});
		};
		$scope.showAlarmDetail = function(guid,sendtype){
			//再去搜索一次
			$scope.queryObject.alarminfo_guid=guid;
			$scope.queryObject.sendtype=sendtype;
			$scope.sendtype=sendtype;
			$waterDataCommon.getPageRows("/Exception/getAlarmInfoSendMessage.do","alarmMessageDetail",$scope.queryObject,10,1);
			$itfreer.openWindow('show_alarm_detail_win');
		}
		$scope.waterDataCommon=$waterDataCommon;
		$scope.NavService = $TaskNavService;
		
		$("#taskQueryMain").hide();
	    $("#taskDetail").show();
	    
		if($wdp.stationListReady == "true"){
			$scope.$emit('askDataPramChange', $scope.reportParms); 
		}
		
		//----------------------------------------
		//报警日志方法
		//----------------------------------------
		//获取报警日志
		$scope.getAlarmLogList = function(){
			if($scope.allStationSelected == true){
				$scope.queryObject.STATION_ID = null;
			}else{
				$scope.queryObject.STATION_ID = $scope.STATION_ID;
			}
			$scope.queryObject._format="json";
			$waterDataCommon.getPageRows("/Exception/getAlarmLogList.do","AlarmLogList",$scope.queryObject,12,1);
		}
		//导出报警日志
		$scope.expAlarmLogList = function(){
			if($scope.allStationSelected == true){
				$scope.queryObject.STATION_ID = null;
			}else{
				$scope.queryObject.STATION_ID = $scope.STATION_ID;
			}
			$scope.queryObject._format="excel";
			var queryString = $waterDataCommon.jsonTostring($scope.queryObject);
			window.open(getBasePath()+"/Exception/getAlarmLogList.do?"+queryString);
		}
		
	});
}(window, angular));